import { checkCaptcha, getCaptcha, getInfo, login, logout, lookAllUser, updateUser, uploadAvatar, register } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'
import store from '..'
const getDefaultState = () => {
  return {
    token: getToken(),
    userInfo: {},
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },

  SET_INFO: (state, user) => {
    state.userInfo = user
  },
}

const actions = {
  // user login
  async login({ commit }, userInfo) {
    const { phone, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ phone: phone.trim(), password: password })
        .then((response) => {
          const { data } = response
          commit('SET_TOKEN', data)
          setToken(data)
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 条件查询
  async lookUser({ commit }, user) {
    return new Promise((resolve, reject) => {
      lookAllUser(user)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 修改用户
  async updateUser({ commit }, user) {
    return new Promise((resolve, reject) => {
      updateUser(user)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 注册
  async register({ commit }, user) {
    return new Promise((resolve, reject) => {
      register(user)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 头像上传
  async uploadAvatar({ commit }, { uid, fid }) {
    return new Promise((resolve, reject) => {
      uploadAvatar(uid, fid)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 获取用户信息
  async getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token)
        .then((response) => {
          const { data, success } = response
          if (!success) {
            store.dispatch('user/logout')
            reject('')
          }
          commit('SET_INFO', data)
          resolve(data)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 退出登录
  async logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then((response) => {
          removeToken() // must remove  token  first
          uni.reLaunch({ url: 'pages/login' })
          commit('RESET_STATE')
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 删除token
  async resetToken({ commit }) {
    return new Promise((resolve) => {
      removeToken()
      commit('RESET_STATE')
      resolve()
    })
  },
  // 获取验证码
  async getCaptcha({ state }, type) {
    return new Promise((resolve, reject) => {
      getCaptcha(type)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 获取验证码
  async checkCaptcha({ state }, { uid, code }) {
    return new Promise((resolve, reject) => {
      checkCaptcha(uid, code)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
